﻿namespace Nomo.CoreModule
{
    /// <summary>
    /// 栈池。
    /// </summary>
    /// <typeparam name="T">栈的成员的类型。</typeparam>
    public static class StackPool<T>
    {
        private static readonly ObjectPool<System.Collections.Generic.Stack<T>> Pool = new ObjectPool<System.Collections.Generic.Stack<T>>(() => new System.Collections.Generic.Stack<T>(), default, e => e.Clear());

        /// <summary>
        /// 从栈池里取出一个空栈。
        /// </summary>
        /// <returns>一个空栈。</returns>
        public static System.Collections.Generic.Stack<T> Get()
        {
            return Pool.Get();
        }

        /// <summary>
        /// 将一个栈放回栈池。
        /// </summary>
        /// <param name="stack">要放回栈池的栈。</param>
        public static void Release(System.Collections.Generic.Stack<T> stack)
        {
            if (stack != null)
            {
                Pool.Release(stack);
            }
        }

        /// <summary>
        /// 移除栈池中的所有栈。
        /// </summary>
        public static void Clear()
        {
            Pool.Clear();
        }
    }
}
